//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.farm.doc.server.utils;

import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.sql.result.DataResult;
import com.farm.doc.domain.Docfiletext;
import com.farm.doc.domain.FarmDocfile;
import com.farm.doc.domain.ex.DocEntire;
import com.farm.doc.domain.ex.FileIndexInfo;
import com.farm.doc.server.FarmDocIndexInter;
import com.farm.doc.server.FarmDocManagerInter;
import com.farm.doc.server.FarmFileManagerInter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class IndexUtils {
    private static Map<String, IndexUtilsDomain> domains = new HashMap();

    public IndexUtils() {
    }

	public static IndexUtilsDomain startReKnowIndexTaskBy(String taskkey, final DataQuery query,
			final FarmDocManagerInter farmDocManagerImpl, final FarmDocIndexInter farmDocIndexManagerImpl) {
        final IndexUtilsDomain domain = new IndexUtilsDomain(0, 0, 0, "", taskkey);
        domains.put(taskkey, domain);
        Thread t = new Thread(new Runnable() {
            public void run() {
                try {
                    List<DocEntire> docs = new ArrayList();
                    DataQuery query1 = farmDocManagerImpl.createSimpleDocQuery(query).addRule(new DBRule("DOMTYPE", "4", "!="));
                    query1.setCurrentPage(1);
                    int i = 1;
                    domain.setState(0);
                    int n = 0;

                    while(true) {
                        try {
                            query1.setPagesize(1000);
                            query1.setCurrentPage(i++);
                            DataResult result = query1.search();
                            domain.setMaxNum(result.getTotalSize());
                            if (result.getResultList().size() <= 0) {
                                break;
                            }

                            Iterator var7 = result.getResultList().iterator();

                            while(var7.hasNext()) {
                                Map<String, Object> node = (Map)var7.next();
                                ++n;
                                domain.setProcessNum(n);
                                DocEntire doc = farmDocManagerImpl.getDoc(node.get("ID").toString());
                                docs.add(doc);
                            }
                        } catch (SQLException var9) {
                            var9.printStackTrace();
                            break;
                        }
                    }

                    domain.setState(1);
                    farmDocIndexManagerImpl.delLuceneIndexs(docs, domain);
                    domain.setState(2);
                    farmDocIndexManagerImpl.addLuceneIndexs(docs, domain);
                    domain.setState(3);
                } catch (Exception var10) {
                    domain.setState(-2);
                    domain.setMessage(var10.getMessage() + var10);
                }

            }
        });
        t.start();
        return domain;
    }

    public static IndexUtilsDomain startReFileIndexTaskBy(String taskkey, final DataQuery query, final FarmFileManagerInter farmFileManagerImpl, final FarmDocManagerInter farmDocManagerImpl, final FarmDocIndexInter farmDocIndexManagerImpl) {
        final IndexUtilsDomain domain = new IndexUtilsDomain(0, 0, 0, "", taskkey);
        domains.put(taskkey, domain);
        Thread t = new Thread(new Runnable() {
            public void run() {
                try {
                    List<FileIndexInfo> fileindexs = new ArrayList();
                    query.setCurrentPage(1);
                    int i = 1;
                    domain.setState(0);
                    int n = 0;

                    while(true) {
                        try {
                            query.setPagesize(1000);
                            query.setCurrentPage(i++);
                            DataResult result = query.search();
                            domain.setMaxNum(result.getTotalSize());
                            if (result.getResultList().size() <= 0) {
                                break;
                            }

                            Iterator var6 = result.getResultList().iterator();

                            while(var6.hasNext()) {
                                Map<String, Object> node = (Map)var6.next();
                                ++n;
                                domain.setProcessNum(n);
                                if (node.get("DOCID") != null && node.get("DOCID").toString().length() >= 32) {
                                    DocEntire doc = farmDocManagerImpl.getDoc(node.get("DOCID").toString());
                                    FarmDocfile file = farmFileManagerImpl.getFile(node.get("ID").toString());
                                    Docfiletext filetext = farmFileManagerImpl.getFiletext(node.get("ID").toString());
                                    fileindexs.add(new FileIndexInfo(file.getId(), file.getName(), filetext.getDescribes(), doc));
                                }
                            }
                        } catch (SQLException var10) {
                            var10.printStackTrace();
                            break;
                        }
                    }

                    domain.setMaxNum(fileindexs.size());
                    domain.setState(1);
                    farmDocIndexManagerImpl.delLuceneIndexsByFile(fileindexs, domain);
                    domain.setState(2);
                    farmDocIndexManagerImpl.addLuceneIndexsByFile(fileindexs, domain);
                    domain.setState(3);
                    domain.setState(3);
                } catch (Exception var11) {
                    domain.setState(-2);
                    domain.setMessage(var11.getMessage() + var11);
                }

            }
        });
        t.start();
        return domain;
    }

    public static IndexUtilsDomain getTaskState(String taskkey) {
        return domains.get(taskkey) != null ? (IndexUtilsDomain)domains.get(taskkey) : new IndexUtilsDomain(0, 0, -1, "-1无任务", taskkey);
    }
}
